package com.cskaoyan.service;

import com.cskaoyan.bean.admin.*;
import com.cskaoyan.mapper.AdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    AdminMapper adminMapper;

    /**
     * 返回全部管理员的信息
     */
    @Override
    public AdminListBean list(Integer page, Integer limit, String username, String sort, String order) {
        PageHelper.startPage(page,limit);
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        adminExample.setOrderByClause(sort + " " + order);
        List<Admin> adminList = adminMapper.selectByExample(adminExample);
        PageInfo pageInfo = new PageInfo(adminList);
        long total = pageInfo.getTotal();

        AdminListBean adminListBean = new AdminListBean();
        adminListBean.setItems(adminList);
        adminListBean.setTotal(total);
        return adminListBean;
    }

    /**
     * 处理创建新管理员请求，返回客户端需要的相关信息
     * @param adminCreateBO
     * @return
     */
    @Override
    public AdminUpdateVO create(Admin admin) {
        if (HadUserByUsername(admin.getUsername())) {
            return new AdminUpdateVO();
        }
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        adminMapper.insertSelective(admin);
        AdminUpdateVO adminCreateVO = adminMapper.selectByUsername(admin.getUsername());
        return adminCreateVO;
    }



    /**
     * 处理更新管理员信息的请求，返回客户端需要的相关信息
     * @param adminUpdateBO
     * @return
     */
//    @Override
//    public AdminUpdateVO update(Admin admin) {
//        if (HadUserByUpdate(admin)) {
//            return new AdminUpdateVO();
//        }
//        admin.setUpdateTime(new Date());
//        adminMapper.updateByPrimaryKeySelective(admin);
//        AdminUpdateVO adminUpdateVO = adminMapper.selectById(admin.getId());
//        return adminUpdateVO;
//    }
    @Override
    public AdminUpdateVO update(Admin admin) {
        if (HadUserByUpdate(admin)) {
            return new AdminUpdateVO();
        }
        admin.setUpdateTime(new Date());
        adminMapper.updateByPrimaryKeySelective(admin);
        AdminUpdateVO adminUpdateVO = adminMapper.selectByUsername(admin.getUsername());
        return adminUpdateVO;
    }
    @Override
    public void delete(Admin admin) {
        adminMapper.deleteByPrimaryKey(admin.getId());
    }

    /**
     * 在创建新管理员时检查是否存在相同的用户名的管理员
     * @param username
     * @return
     */
    private boolean HadUserByUsername(String username) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        long count = adminMapper.countByExample(adminExample);
        return count != 0 ? true:false;
    }

    /**
     * 在更新管理员信息时检查是否存在用户名相同，但id不同的管理员。存在则返回错误提示。
     * @param adminUpdateBO
     * @return
     */
    private boolean HadUserByUpdate(Admin admin) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername()).andIdNotEqualTo(admin.getId());
        long count = adminMapper.countByExample(adminExample);
        return count != 0 ? true:false;
    }
}
